var express = require('express');
var router = express.Router();
var connection = require('../db/sql')

//设置允许跨域访问该服务.

router.all('', function(req, res, next) {
	res.header('Access-Control-Allow-Origin', '*');
	//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
	res.header('Access-Control-Allow-Headers', 'Content-Type');
	res.header('Access-Control-Allow-Methods', '*');
	res.header('Content-Type', 'application/json;charset=utf-8');
	next();
});


/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

// ****商品详情页
router.get('/api/goods/id', function(req, res, next) {
	// 测试路由：   http://localhost:3000/api/goods/2
	// console.log(req.query);
	let id = req.query.id;
	// console.log(id);

	connection.query("SELECT * FROM goods_search WHERE id=" + id + "", function(error, results, fields) {
		if (error) throw error;
		// console.log('The solution is: ', results);
		res.send({
			code: "0",
			data: results
		})
	})
});

// ****搜索页
router.get('/api/goods/search', function(req, res, next) {
	// 测试路由：   http://localhost:3000/api/goods/search?name=大&price_new=desc
	// 降序/升序：desc/asc
	// 解构路由中的关键字的key
	let [goodsName, orderName] = Object.keys(req.query);
	// console.log(req.query);
	// console.log(Object.keys(req.query));
	// console.log(goodsName,orderName);

	// 获取goodsName的key的值
	let name = req.query[goodsName];
	// 获取orderName的key的值
	let order = req.query[orderName];
	// console.log(name,order);

	connection.query("SELECT * FROM goods_search WHERE name LIKE '%" + name + "%' ORDER BY " + orderName + " " +
		order + "",
		function(error, results, fields) {
			if (error) throw error;
			// console.log('The solution is: ', results);
			res.send({
				code: "0",
				data: results
			})
		})
});

// ****首页
// 大牌潮流模块数据第一次触底
router.get('/api/index/2/data/2', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
			type: "commodityLis",
			data: [{
				id: 1,
				imgUrl: "../../static/img/commodity1.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 2,
				imgUrl: "../../static/img/commodity2.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 3,
				imgUrl: "../../static/img/commodity3.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 4,
				imgUrl: "../../static/img/commodity4.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}],
		}]
	})
});
// 大牌潮流模块数据第二次触底
router.get('/api/index/2/data/3', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
			type: "commodityLis",
			data: [{
				id: 1,
				imgUrl: "../../static/img/commodity1.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 2,
				imgUrl: "../../static/img/commodity2.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 3,
				imgUrl: "../../static/img/commodity3.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 4,
				imgUrl: "../../static/img/commodity4.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}],
		}]
	})
});
// 大牌潮流模块数据首次
router.get('/api/index/2/data/1', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
				type: "bannerList",
				imgUrl: "../../static/img/banner1.jpg",
			},
			{
				type: "commodityLis",
				data: [{
					id: 1,
					imgUrl: "../../static/img/commodity1.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 2,
					imgUrl: "../../static/img/commodity2.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 3,
					imgUrl: "../../static/img/commodity3.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 4,
					imgUrl: "../../static/img/commodity4.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}],
			}
		]
	})
});
// 休闲生活模块数据
router.get('/api/index/3/data/1', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
				type: "bannerList",
				imgUrl: "../../static/img/banner1.jpg",
			},
			{
				type: "commodityLis",
				data: [{
					id: 1,
					imgUrl: "../../static/img/commodity1.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 2,
					imgUrl: "../../static/img/commodity2.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 3,
					imgUrl: "../../static/img/commodity3.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 4,
					imgUrl: "../../static/img/commodity4.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}],
			}
		]
	})
});
// 推荐模块第二次触底
router.get('/api/index/1/data/3', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
			type: "commodityLis",
			data: [{
				id: 1,
				imgUrl: "../../static/img/commodity1.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 2,
				imgUrl: "../../static/img/commodity2.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 3,
				imgUrl: "../../static/img/commodity3.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 4,
				imgUrl: "../../static/img/commodity4.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}],
		}]
	})
});
// 推荐模块第一次触底
router.get('/api/index/1/data/2', function(req, res, next) {
	res.json({
		"code": 0,
		"data": [{
			type: "commodityLis",
			data: [{
				id: 1,
				imgUrl: "../../static/img/commodity1.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 2,
				imgUrl: "../../static/img/commodity2.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 3,
				imgUrl: "../../static/img/commodity3.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}, {
				id: 4,
				imgUrl: "../../static/img/commodity4.jpg",
				name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
				price_new: "299",
				price_old: "599",
				discount: "5.2",
			}],
		}]
	})
});
// 推荐模块数据
router.get('/api/index/data', function(req, res, next) {
	// res.header("Access-Control-Allow-Origin", "*");
	res.json({
		"code": 0,
		"data": {
			topBar: [{
					Id: 1,
					name: "推荐"
				},
				{
					Id: 2,
					name: '大牌潮流'
				},
				{
					Id: 3,
					name: '休闲生活'
				},
				{
					Id: 4,
					name: '运动实战'
				},
				{
					Id: 5,
					name: '硬核户外'
				},
				{
					Id: 6,
					name: '鞋类配件'
				}
			],
			data: [{
				type: "swiperList",
				data: [{
						imgUrl: '../../static/img/swiper1.jpg'
					},
					{
						imgUrl: '../../static/img/swiper2.jpg'
					},
					{
						imgUrl: '../../static/img/swiper3.jpg'
					},
				]
			}, {
				type: "iconsList",
				data: [{
						imgUrl: '../../static/img/icons/icons3.png',
						name: '跑鞋'
					},
					{
						imgUrl: '../../static/img/icons/icons4.png',
						name: '篮球鞋'
					},
					{
						imgUrl: '../../static/img/icons/icons5.png',
						name: '足球鞋'
					},
					{
						imgUrl: '../../static/img/icons/icons6.png',
						name: '板鞋'
					},
					{
						imgUrl: '../../static/img/icons/icons7.png',
						name: '帆布鞋'
					},
					{
						imgUrl: '../../static/img/icons/icons8.png',
						name: '拖鞋'
					},
				]
			}, {
				type: "recommendList",
				data: [{
						bigImgUrl: '../../static/img/Children.jpg',
						data: [{
							imgUrl: '../../static/img/Children1.jpg'
						}, {
							imgUrl: '../../static/img/Children2.jpg'
						}, {
							imgUrl: '../../static/img/Children3.jpg'
						}]
					},
					{
						bigImgUrl: '../../static/img/Furnishing.jpg',
						data: [{
							imgUrl: '../../static/img/Furnishing1.jpg'
						}, {
							imgUrl: '../../static/img/Furnishing2.jpg'
						}, {
							imgUrl: '../../static/img/Furnishing3.jpg'
						}]
					},
				]
			}, {
				type: "commodityLis",
				data: [{
					id: 1,
					imgUrl: "../../static/img/commodity1.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 2,
					imgUrl: "../../static/img/commodity2.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 3,
					imgUrl: "../../static/img/commodity3.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}, {
					id: 4,
					imgUrl: "../../static/img/commodity4.jpg",
					name: "精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款精致羽绒服2022款",
					price_new: "299",
					price_old: "599",
					discount: "5.2",
				}]
			}]
		},
	})
})

module.exports = router;
